
var BattleSword = cc.Node.extend({

    side : true,    // 站位 Left : true, Right : false
    swrodImgFile : '',
	swordImg : null,

    ctor : function (image) {
		// needed for JS-Bindings compatibility
        cc.associateWithNative( this, cc.Node );

    	// 创建飞剑图像
    	this.swrodImgFile = image;
        this.swordImg = cc.Sprite.create(image);
    	this.addChild(this.swordImg);

    	//this.setScale(0.1);
        // this.setPositionX(100);

        this.aniInit(true);
        this.aniIdle();
	},


	// 正反两面
    setSide : function (isLeft) {
        if(isLeft == false) this.setScaleX(-1);
        else this.setScaleX(1);
        this.side = isLeft;
    },

    // 播放动作
    // 初始状态
    aniInit : function (isImmediately, onOverFunc, target) {
        this.swordImg.stopAllActions();
        
        if (isImmediately) {
            // 立即回到原始位置
            this.swordImg.setRotation(70);
            this.swordImg.setPosition(0,0);           
        } else {
            // 播放动画回到其实位置
            var rot = cc.EaseSineOut.create( cc.RotateTo.create(0.3, 70) );  // 角度
            var move = cc.EaseSineOut.create( cc.MoveTo.create(0.3, cc.p(0,0)) );    // 位置

            if (! onOverFunc) {
                this.swordImg.runAction(cc.Spawn.create(rot, move));
            } else {
                var overFunc = cc.CallFunc.create(onOverFunc, target);  // 结束回调
                this.swordImg.runAction(cc.Sequence.create(cc.Spawn.create(rot, move), overFunc));
            }
        }
    },

    aniIdle : function () {

        var _func = function () {
            var time = 1.2;
            var height = 30;
            var up = cc.EaseSineInOut.create( cc.MoveTo.create(time, {x:0, y:height}) );   // 往上
            var down = cc.EaseSineInOut.create( cc.MoveTo.create(time, {x:0, y:0}) );      // 往下
            var repeat = cc.RepeatForever.create(cc.Sequence.create(up, down));     // 永远执行序列
            this.swordImg.runAction(repeat);
        };

        this.aniInit(false, _func, this);
    },

    aniAttack : function () {

        var _func = function () {
            var rot = cc.RotateTo.create(0.2, 0);  // 角度:水平
            var rush = cc.EaseBackIn.create( cc.MoveBy.create(1.2, {x:1200, y:0}) );    // 前进 修饰:先回退一下
            var place = cc.Place.create({x:-120, y:0});      // 回到后方
            var overFunc = cc.CallFunc.create(this.onAniAttackOver, this);  // 结束回调
            this.swordImg.runAction(cc.Sequence.create(rot, rush, place, overFunc));
        };

        this.aniInit(false, _func, this);
    },

    _onAniAttackOver : function () {
        this.aniIdle();
    },

    aniHurt : function () {

        var _func = function () {
            var red = cc.TintTo.create(0.01, 255,64,64);
            var white = cc.TintTo.create(0.01, 255,255,255);
            var d = cc.DelayTime.create(0.12);
            var overFunc = cc.CallFunc.create(this._onAniAttackOver, this);  // 结束回调
            this.swordImg.runAction(cc.Sequence.create(red, d, white, d, red, d, white, d, red, d, white, overFunc));
        };

        this.aniInit(false, _func, this);
    },

    aniAniSkill : function () {
        this.onAniSkillType1();
    },


    aniAniSkillType1 : function () {

        // 创建小剑
        var SUB_SWORDS_NUM = 10;
        var subSwords = [];
        for(var i=0; i<SUB_SWORDS_NUM; i++) {
            var s = cc.Sprite.create(this.swrodImgFile);
            s.setScale(0.5);
            s.setOpacity(200);
            this.addChild(s);
            subSwords.push(s);
        }
                
        var _func = function () {
            for(var i=0; i<SUB_SWORDS_NUM; i++) {
                var s = subSwords[i];
                
                var move1 = cc.MoveTo.create(0.7, cc.p(-50, 160 - 12 - 33 * i));
                var move2 = cc.MoveTo.create(1.2, cc.p(1000, 0));
                var d = cc.DelayTime.create(1);
                var overFunc = cc.CallFunc.create(_onAniAniSkillType1Over, this);  // 结束回调
                s.runAction(cc.Sequence.create(move1, d, move2, overFunc));
            }
        };


        this.aniInit(false, _func, this);

        var _onAniAniSkillType1Over = function () {
            // 清理小飞剑
        };
    },

});

